package com.kalvin.kvf.modules.hxy.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kalvin.kvf.common.utils.CamelToUnderscoreConverter;
import com.kalvin.kvf.common.utils.ShiroKit;
import com.kalvin.kvf.modules.hxy.mapper.KeywordMapper;
import com.kalvin.kvf.modules.sys.entity.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.kalvin.kvf.common.controller.BaseController;
import com.kalvin.kvf.common.dto.R;
import com.kalvin.kvf.modules.hxy.entity.Keyword;
import com.kalvin.kvf.modules.hxy.service.KeywordService;

import java.time.LocalDateTime;
import java.util.List;


/**
 * <p>
 * 关键字 前端控制器
 * </p>
 * @since 2023-10-28 20:35:19
 */
@RestController
@RequestMapping("hxy/keyword")
public class KeywordController extends BaseController {

    @Autowired
    private KeywordMapper keywordMapper;

    @Autowired
    private KeywordService keywordService;

    @RequiresPermissions("hxy:keyword:index")
    @GetMapping("index")
    public ModelAndView index() {
        return new ModelAndView("hxy/keyword");
    }

    @GetMapping(value = "edit")
    public ModelAndView edit(Long id) {
        ModelAndView mv = new ModelAndView("hxy/keyword_edit");
        Keyword keyword;
        if (id == null) {
            keyword = new Keyword();
        } else {
            keyword = keywordService.getById(id);
        }
        mv.addObject("editInfo", keyword);
        return mv;
    }

    @GetMapping(value = "list/data")
    public R listData(Keyword keyword) {
        Page<Keyword> page = keywordService.listKeywordPage(keyword);
        return R.ok(page);
    }

    @RequiresPermissions("hxy:keyword:add")
    @PostMapping(value = "add")
    public R add(Keyword keyword) {
        User user = ShiroKit.getUser();
        keyword.setPhone(user.getUsername());
        keyword.setCreateUser(user.getUsername());
        keyword.setCreateTime(LocalDateTime.now());
        keywordService.save(keyword);
        return R.ok();
    }

    @RequiresPermissions("hxy:keyword:del")
    @PostMapping(value = "batchdel")
    public R batchdel(@RequestParam("ids") List<Long> ids) {
        keywordService.removeByIds(ids);
        return R.ok();
    }

    @RequiresPermissions("hxy:keyword:edit")
    @PostMapping(value = "edit")
    public R edit(Keyword keyword) {
        User user = ShiroKit.getUser();
        keyword.setPhone(user.getUsername());
        keyword.setUpdateUser(user.getUsername());
        keyword.setUpdateTime(LocalDateTime.now());
        keywordService.updateById(keyword);
        return R.ok();
    }

    @RequiresPermissions("hxy:keyword:del")
    @PostMapping(value = "del/{id}")
    public R del(@PathVariable Long id) {
        keywordService.removeById(id);
        return R.ok();
    }

    @GetMapping(value = "get/{id}")
    public R get(@PathVariable Long id) {
        return R.ok(keywordService.getById(id));
    }



    @PostMapping("/getKeywords")
    public R getKeywords(Long keywordType){
        List<String> keywords = keywordService.selectKeywords(keywordType);
        if(keywords.size() == 0){
            return R.fail("请添加关键字!");
        }
        return R.ok(keywords);
    }

    @RequiresPermissions("hxy:keyword:edit")
    @PostMapping(value = "edit_one")
    public R editOne(String id,String field,String value) {
        //keywordName  转   keyword_name
        field = CamelToUnderscoreConverter.camelToUnderscoreConverter(field);
        //修改的对象
        Keyword keyword = keywordMapper.selectById(id);
        User user = ShiroKit.getUser();
        keyword.setPhone(user.getUsername());
        keyword.setUpdateUser(user.getUsername());
        keyword.setUpdateTime(LocalDateTime.now());
        //构建修改条件
        UpdateWrapper<Keyword> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set(field,value).lambda();
        updateWrapper.lambda().eq(Keyword::getId,id);
        //修改
        keywordMapper.update(keyword,updateWrapper);
        return R.ok();
    }

}

